package com.remenote.remenoteback.context.interceptor;

import com.remenote.remenoteback.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @BelongsProject: remenote-back
 * @BelongsPackage: com.remenote.remenoteback.context.interceptor
 * @Author: 23392
 * @CreateTime: 2024-03-18  14:41
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class AuthorityInterceptor implements HandlerInterceptor {
    @Autowired
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        String username = request.getHeader("username");
        if (token == null || !userService.validate(username, token).isSuccess()) {
            throw new RuntimeException("No authority");
        }
        return true;
    }
}
